package com.google.code.netbuilder.dal.providersimpl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class EquipmentProvider {

	public Object findByID(Integer id)throws SQLException{
		
		Connection conn = DriverManager.getConnection(
	            "jdbc:mysql://localhost:3306/netbuilderDB",
	            "root", "1234");
	 
	        if (conn == null) {
	            System.out.println("No DB connection!!!");
	            throw new SQLException("No connection found");
	            
	        }
	        if (conn != null){
	        	System.out.println("Connection is here: " + conn.getAutoCommit());
	        	
	        }
	 
	        Statement stmt = conn.createStatement();
	        ResultSet rs = null;
	        
			try {
				rs = stmt.executeQuery("SELECT * FROM equipment where id ="
						+ id.toString());
	
				while (rs.next()) {
					System.out.println("Equipment with id = " + id.toString()
							+ " found!");
					Integer idres = rs.getInt("id");
					String name = rs.getString("name");
					Integer portNumber = rs.getInt("number_of_ports");
					System.out.println("ID: " + idres + ", name: " + name
							+ ", number of ports: " + portNumber);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			finally {
				try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
				try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); }
				try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
			}
				
	    
		return rs;
		
	}
}
